import { ref } from 'vue'
import { info, update } from '@/api/user'
import { get as getUnion, update as updateUnion } from '@/api/union'

// 数据
export const userData = ref({})
export const unionData = ref({})

// 获取信息
export const userInfo = () => {
  return new Promise((resolve, reject) => {
    info().then(res => {
      userData.value = res.data
      resolve(userData.value)
    }).catch(err => {
      reject(err)
    })
  })
}

// 修改信息
export const userUpdate = data => {
  return new Promise((resolve, reject) => {
    update(data).then(res => {
      userInfo()
      resolve()
    }).catch(err => {
      reject(err)
    })
  })
}

// 我的工会
export const unionInfo = () => {
  return new Promise((resolve, reject) => {
    getUnion(userData.value.union_id).then(res => {
      unionData.value = res.data
      resolve(unionData.value)
    }).catch(err => {
      reject(err)
    })
  })
}

// 修改工会信息
export const unionUpdate = data => {
  return new Promise((resolve, reject) => {
    updateUnion(data).then(res => {
      unionInfo()
      resolve()
    }).catch(err => {
      reject(err)
    })
  })
}

// 判断有没有工会
export const unionHas = () => {
  return Number(userData.value.union_id) > 0
}